home *** CD-ROM | disk | FTP | other *** search
- //***********************************************************************
- //
- // DlgDemo2.cpp
- //
- //***********************************************************************
-
- #include <afxwin.h>
- #include "Resource.h"
- #include "DlgDemo2.h"
-
- #define FONTHEIGHT 72
-
- CMyApp myApp;
-
- /////////////////////////////////////////////////////////////////////////
- // CMyApp member functions
-
- BOOL CMyApp::InitInstance ()
- {
- m_pMainWnd = new CMainWindow;
- m_pMainWnd->ShowWindow (m_nCmdShow);
- m_pMainWnd->UpdateWindow ();
- return TRUE;
- }
-
- /////////////////////////////////////////////////////////////////////////
- // CMainWindow message map and member functions
-
- BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
- ON_WM_ERASEBKGND ()
- ON_WM_PAINT ()
- ON_COMMAND (IDM_OPTIONS_EXIT, OnOptionsExit)
- ON_COMMAND (IDM_OPTIONS_ABOUT, OnOptionsAbout)
- END_MESSAGE_MAP ()
-
- CMainWindow::CMainWindow ()
- {
- Create (NULL, "DlgDemo2", WS_OVERLAPPEDWINDOW, rectDefault, NULL,
- MAKEINTRESOURCE (IDR_MAINFRAME));
- }
-
- BOOL CMainWindow::OnEraseBkgnd (CDC* pDC)
- {
- CRect rect;
- GetClientRect (&rect);
- DoGradientFill (pDC, &rect);
- return TRUE;
- }
-
- void CMainWindow::OnPaint ()
- {
- CRect rect;
- GetClientRect (&rect);
-
- CPaintDC dc (this);
- DoDrawText (&dc, &rect);
- }
-
- void CMainWindow::OnOptionsExit ()
- {
- SendMessage (WM_CLOSE, 0, 0);
- }
-
- void CMainWindow::OnOptionsAbout ()
- {
- CAboutDialog dlg (this);
- dlg.DoModal ();
- }
-
- void CMainWindow::DoGradientFill (CDC* pDC, CRect* pRect)
- {
- CBrush* pBrush[64];
- for (int i=0; i<64; i++)
- pBrush[i] = new CBrush (RGB (0, 0, 255 - (i * 4)));
-
- int nWidth = pRect->Width ();
- int nHeight = pRect->Height ();
- CRect rect;
-
- for (i=0; i<nHeight; i++) {
- rect.SetRect (0, i, nWidth, i + 1);
- pDC->FillRect (&rect, pBrush[(i * 63) / nHeight]);
- }
-
- for (i=0; i<64; i++)
- delete pBrush[i];
- }
-
- void CMainWindow::DoDrawText (CDC* pDC, CRect* pRect)
- {
- CFont font;
- int nHeight = -((pDC->GetDeviceCaps (LOGPIXELSY) * FONTHEIGHT) / 72);
-
- font.CreateFont (nHeight, 0, 0, 0, FW_BOLD, TRUE, 0, 0,
- DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
- DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Times New Roman");
-
- pDC->SetBkMode (TRANSPARENT);
- pDC->SetTextColor (RGB (255, 255, 255));
-
- CFont* pOldFont = pDC->SelectObject (&font);
- pDC->DrawText ("Hello, MFC", -1, pRect, DT_SINGLELINE | DT_CENTER |
- DT_VCENTER);
-
- pDC->SelectObject (pOldFont);
- }
-
- /////////////////////////////////////////////////////////////////////////
- // CAboutDialog message map and member functions
-
- BEGIN_MESSAGE_MAP (CAboutDialog, CDialog)
- ON_WM_PAINT ()
- END_MESSAGE_MAP ()
-
- BOOL CAboutDialog::OnInitDialog ()
- {
- CDialog::OnInitDialog ();
-
- CStatic* pStatic = (CStatic*) GetDlgItem (IDC_ICONRECT);
- pStatic->GetWindowRect (&m_rect);
- pStatic->DestroyWindow ();
- ScreenToClient (&m_rect);
-
- return TRUE;
- }
-
- void CAboutDialog::OnPaint ()
- {
- CPaintDC dc (this);
- HICON hIcon = (HICON) ::GetClassLong (AfxGetMainWnd ()->m_hWnd,
- GCL_HICON);
-
- if (hIcon != NULL) {
- CDC dcMem;
- dcMem.CreateCompatibleDC (&dc);
-
- CBitmap bitmap;
- bitmap.CreateCompatibleBitmap (&dc, 32, 32);
- CBitmap* pOldBitmap = dcMem.SelectObject (&bitmap);
-
- CBrush brush (::GetSysColor (COLOR_3DFACE));
- dcMem.FillRect (CRect (0, 0, 32, 32), &brush);
- dcMem.DrawIcon (0, 0, hIcon);
-
- dc.StretchBlt (m_rect.left, m_rect.top, m_rect.Width(),
- m_rect.Height (), &dcMem, 0, 0, 32, 32, SRCCOPY);
-
- dcMem.SelectObject (pOldBitmap);
- }
- }
-